import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import path from 'path'; // 确保导入 path 模块

const loadEnvConfig = (mode) => loadEnv(mode, process.cwd());

export default defineConfig(({ command, mode }) => {
    const env = loadEnvConfig(mode);

    return {
        server: {
            host: '0.0.0.0',
            port: 8443,
            proxy: {
                '/api': {
                    target: env.VITE_API_HOST,
                    changeOrigin: true,
                    ws: true,
                    secure: false,
                    rewrite: (path) => path.replace(/^\/api/, ''),
                },
                '/zyq': {
                    target: `${env.VITE_API_HOST}/zyq`,
                    changeOrigin: true,
                    ws: true,
                    secure: false,
                    rewrite: (path) => path.replace(/^\/zyq/, ''),
                },
                '/ai_s': {
                    target: `${env.VITE_API_HOST}/ai_s`,
                    changeOrigin: true,
                    ws: true,
                    secure: false,
                    rewrite: (path) => path.replace(/^\/ai_s/, ''),
                },
                
                '/aix': {
                    target: env.VITE_API_HOST,
                    changeOrigin: true,
                    ws: true,
                    secure: false,
                    rewrite: (path) => path.replace(/^\/aix/, ''),
                },
                
                // '/': {
                //     target: env.VITE_API_HOST,
                //     changeOrigin: true,
                //     ws: true,
                //     secure: false,
                //     rewrite: (path) => path.replace(/^\//, ''),
                // },
            },
        },
        optimizeDeps: {
            include: ['element-plus/es/locale/lang/zh-cn'],
        },
        plugins: [
            vue(),
            // 移除 defineOption()
            createSvgIconsPlugin({
                // 指定目录(svg存放目录）
                iconDirs: [path.resolve(process.cwd(), "src/icons")],
                // 使用 svg 图标的格式（name为图片名称）
                symbolId: "icon-[dir]-[name]",
            })
        ],
        resolve: {
            alias: {
              '@': path.resolve(__dirname, 'src') // 别名，简化路径
            },
            extensions: ['.js', '.vue', '.json'] // 确保支持 .vue 扩展名
          }
    };
});